<?php
/**
 * Class name  is ConfigVariableController * @package backend\modules\common\controllers;
 * @author  whaibo  email:1316177158@qq.com
 * @DateTime 2020/12/10 10:13
 */
namespace backend\modules\common\controllers;

use Yii;
use common\models\config\ConfigVariable;
use common\models\config\ConfigVariableSearch;
use backend\controllers\BaseController;
use common\utils\ToolUtil;
use yii\web\NotFoundHttpException;


class ConfigVariableController extends BaseController{
    //设置全局变量首页
    public function actionIndex(){
        if (Yii::$app->request->isAjax) {
            $this->getConfigVariableLest();
        }else{
            return $this->render('index');
        }
    }

    /**
     * 获取Constant的所有数据
     *
     * @return void
     */
    public function actionList(){       
        
        $searchModel = new ConfigVariableSearch();
        $searchModel->scenario = 'search';
        $dataProvider = $searchModel->search(Yii::$app->request->getBodyParams());
        if($searchModel->hasErrors()){
            $return=ToolUtil::returnAjaxMsg(false,'参数有误', ['errors'=>$searchModel->errors]);
            return $this->asJson($return);
        }

        
        $count = $dataProvider->getTotalCount();        
        return $this->asJson([
            'code' => 0,
            'msg' => '',
            'count' => (int)$count,
            'data' => $dataProvider->getModels(),
        ]);


    }



    //获取全局变量列表
    public function getConfigVariableLest(){
        $this->pageSize = Yii::$app->request->get('limit',5);
        $this->page = Yii::$app->request->get('page',1);
        $parames = Yii::$app->request->get('parames',"");
        $query = ConfigVariable::getVariableList($parames);
        $this->sidx = 'created_at';
        return $this->getJqTableData($query,"");
    }



    //添加全局变量
    public function actionCreate(){
        $request = Yii::$app->request;      

        //如果是表单get提交,如果有参数id,则是更改,没有则是新增
        if($request->isGet){
            $id = $this->get('id');
            if(empty($id)){
                $model = new ConfigVariable();  
                return $this->render('create', [
                ]);
            }else{
                $model = $this->findModel($id);
                return $this->render('create', [
                    'model' => $model,
                ]);

            }
        }

        if($request->isPost){
            //$id = yii::$app->request->post()["ConfigBase"]["id"];
            $id = yii::$app->request->post()["id"];

            //如果没有id,说明是新增
            if(empty($id)){
                $model = new ConfigVariable();  
                if ($model->load($request->post()) && $model->save()) {                   
                    return ToolUtil::returnAjaxMsg(true,'添加成功');
                }else{
                    return ToolUtil::returnAjaxMsg(false,'添加失败');
                }

            }else{
                $model = $this->findModel($id);
                if ($model->load($request->post()) && $model->save()){
                    $return=ToolUtil::returnAjaxMsg(true,'修改成功');
                }else{
                    $return=ToolUtil::returnAjaxMsg(false,'修改失败');
                }
                return $this->asJson($return);
            }

        }


        /* if (Yii::$app->request->isAjax) {
            $request = Yii::$app->request->post();
            $res = ConfigVariable::createDo($request);
            if($res === true){
                $this->returnSuccess('','success');
            }else{
                $this->returnError('',$res);
            }
        }else{
            return $this->render('create');
        } */
    }
    //全局变量修改
    public function actionUpdate(){
        if (Yii::$app->request->isAjax) {
            $request = Yii::$app->request->post();
            $res = ConfigVariable::updateDo($request);
            if($res === true){
                $this->returnSuccess('','success');
            }else{
                $this->returnError('',$res);
            }
        }else{
            $id = Yii::$app->request->get('id',"");
            $model = ConfigVariable::findOne($id);
            return $this->render('update',['data'=>$model]);
        }
    }
    //修改使用状态
    public function actionUpdateStatus(){
        $id = Yii::$app->request->post('id',"");
        $status = Yii::$app->request->post('status',"0");
        $model = ConfigVariable::findOne($id);
        $model->status = $status;
        if ($model->save()) {
            $this->returnSuccess('','success');
        } else {
            $this->returnError('',json_encode($model->getErrors(),JSON_UNESCAPED_UNICODE));
        }
    }
    //删除全局变量 --修改is_del状态（0可用 1删除）
    public function actionDelete(){
        $id = Yii::$app->request->post('id',"");
        $exam = ConfigVariable::find()->where(['is_del'=>0,'id'=>$id,'status'=>1])->one();
        $model = ConfigVariable::findOne($id);
        $model->is_del = 1;
        if(!empty($exam)){
            $this->returnError('','该变量正在被使用不可删除');
        }else{
            if ($model->save()) {
                $this->returnSuccess('','success');
            } else {
                $this->returnError('',json_encode($model->getErrors(),JSON_UNESCAPED_UNICODE));
            }
        }
    }
    //批量删除
    public function actionDeleteAll(){
        $id = Yii::$app->request->get('id',"");
        $array = explode(',',$id);
        unset($array[0]);
        if(!empty($array)){
            foreach ($array as $key => $value) {
                //判断有没有频道或者栏目在使用
                $isUser = ConfigVariable::find()->where(['is_del'=>0,'id'=>$value,'status'=>1])->one();
                if(empty($isUser)) {
                    $model = ConfigVariable::findOne($value);
                    $model->is_del = 1;
                    $model->save();
                }
            }   
            $this->returnSuccess('','success');
        }else{
            $this->returnError('','请选择要删除的数据');
        }
    }

     /**
     * Finds the ConfigConstant model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ConfigConstant the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ConfigVariable::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}
